package com.lanxiao.cloud.modules.dict.controller;

import com.lanxiao.cloud.common.core.dto.PageQuery;
import com.lanxiao.cloud.common.core.dto.ResultModel;
import com.lanxiao.cloud.common.core.enums.OperationType;
import com.lanxiao.cloud.common.web.invalid.Log;
import com.lanxiao.cloud.modules.dict.application.DictQueryManager;
import com.lanxiao.cloud.modules.dict.application.DictTypeCmdManager;
import com.lanxiao.cloud.modules.dict.application.DictTypeQueryManager;
import com.lanxiao.cloud.modules.dict.application.dto.DictTypeCmdAdd;
import com.lanxiao.cloud.modules.dict.application.dto.DictTypeCmdUpdate;
import com.lanxiao.cloud.modules.dict.application.dto.DictTypePageDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：qiaoliang
 * @date ：2021-09-26
 */
@Api(tags = "字典服务")
@Validated
@RequestMapping("/dicts/type")
@RestController
public class DictTypeController {

  private final DictTypeQueryManager dictTypeQueryManager;
  private final DictTypeCmdManager dictTypeCmdManager;

  public DictTypeController(
      DictTypeQueryManager dictTypeQueryManager,
      DictTypeCmdManager dictTypeCmdManager) {
    this.dictTypeQueryManager = dictTypeQueryManager;
    this.dictTypeCmdManager = dictTypeCmdManager;
  }

  @ApiOperation(value = "查询所有字典类型")
  @GetMapping
  public ResponseEntity<ResultModel<DictTypePageDto>> page(String type, PageQuery pageQuery) {
    return ResponseEntity.ok(dictTypeQueryManager.page(type, pageQuery));
  }

  @Log(operationType = OperationType.CREATE)
  @ApiOperation(value = "新增字典类型")
  @PostMapping
  public ResponseEntity<?> save(@Valid @RequestBody DictTypeCmdAdd cmdAdd){
    dictTypeCmdManager.save(cmdAdd);
    return ResponseEntity.status(HttpStatus.CREATED).build();
  }

  @Log(operationType = OperationType.UPDATE, serviceClass = DictTypeQueryManager.class)
  @ApiOperation(value = "修改字典类型")
  @PutMapping("/{id}")
  public ResponseEntity<?> update(@PathVariable Long id, @Valid @RequestBody DictTypeCmdUpdate cmdUpdate){
    dictTypeCmdManager.update(id, cmdUpdate);
    return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT).build();
  }

  @Log(operationType = OperationType.DELETE)
  @ApiOperation(value = "删除字典类型")
  @DeleteMapping("/{id}")
  public ResponseEntity<?> delete(@PathVariable Long id){
    dictTypeCmdManager.delete(id);
    return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
  }

}
